/*************************************************************************
	> File Name: BaseAlgo.h
	> Author: 
	> Mail: 
	> Created Time: 2018/1/20 sat 15:12
	> Version     :
 ************************************************************************/

#ifndef _BASEALGO_H
#define _BASEALGO_H

#include <stdint.h>
#include "AwinicLog.h"

#if 0
#define  GetMinValue(var1,var2) Q6_R_min_RR(var1,var2)
#define  GetMaxValue(var1,var2) Q6_R_max_RR(var1,var2)
#else
#define  GetMaxValue(a,b) (((a)>=(b))? (a):(b))
#define  GetMinValue(a,b) (((a)<=(b))? (a):(b))
#endif

//float log10fApprox(float inData);
//float pow10fApprox(float inData);
int32_t absoluteInt32(int32_t inData);
int32_t int32Mulint32(int32_t inData1,int32_t inData2,int16_t factorBit);
int32_t int32RightShift(int32_t inData,int16_t shiftBit);
int64_t int64RightShift(int64_t inData,int16_t shiftBit);
void write_pcm(char* name,int32_t *buffer,int dataSize);
void write_pcm16bit(char* name,int16_t *buffer,int dataSize);


//Algorithm parameter EXPANDER bit number is 20
#define ALGOPARAMFACTORBIT (20)

/********************Filter Algorithm  *************************************************** **********/
#define FILTERCOECNT  5
#define FILTERDATACNT 2
#define FTPARAMFACTORBIT (ALGOPARAMFACTORBIT)  //Filter Parameter Bit number
typedef struct filterParams{
    int32_t coe[FILTERCOECNT];
}ftParams_t;
typedef struct filterData{
    int32_t inData[FILTERDATACNT];
    int32_t outData[FILTERDATACNT];
}ftData_t;
typedef struct filterCfg{
    ftParams_t params;
    ftData_t data;
    uint16_t paramsFactorBit;
}ftCfg_t;
void filterProcess(int32_t* data,ftCfg_t *cfg,uint16_t wordsCnt);
void filterInit(ftCfg_t *cfg);
void filterReset(ftCfg_t *cfg);
void filterSetParams(ftCfg_t *cfg,ftParams_t *params);
void filterGetParams(ftCfg_t *cfg,ftParams_t *params);
/********************Filter Algorithm  End*************************************************** **********/


/******************** PeakDetector Algorithm  *************************************************** **********/
typedef struct peakDetectParams{
    int32_t holdAttr;
    int32_t holdRel;
}peakDetParams_t;
typedef struct peakDetectData{
    int32_t compValue;
    int32_t sectionPeakValue;
    int32_t dataCount;
}peakDetData_t;

typedef struct peakDetectCfg{
    peakDetParams_t params;
    peakDetData_t data;
}peakDetCfg_t;

void peakDetectorProcess(int32_t*data,peakDetCfg_t *cfg,uint16_t wordsCnt);
void peakDetectorInit(peakDetCfg_t *cfg);
void peakDetectorReset(peakDetCfg_t *cfg);
void peakDetSetParams(peakDetCfg_t *cfg,peakDetParams_t *params);
void peakDetGetParams(peakDetCfg_t *cfg,peakDetParams_t *params);
/********************PeakDetector Algorithm  End*************************************************** **********/

/******************** Gain Curve Algorithm  *************************************************** **********/
#define CURVE_PARAM_BIT (ALGOPARAMFACTORBIT)  //Curve Parameter Bit
#define CURVE_MIN  1
#define CURVE_INDATA_BIT (15)     //Input Data bit Number
typedef enum curveMode{
    COMPRESS,
    EXPANDER,
    NOISEGAT,
    HARDHALF,
}curveMode_t;

typedef struct curveParams{
    int32_t threshold;
    int32_t ratio;
    int32_t knee;
    int32_t slope;
    int32_t width;
    int32_t gain;
}curveParams_t;

typedef struct curveData{
    curveMode_t mode;
    uint16_t paramFactorBit;
    int32_t logCoe;      //convert to db
    float   logSub;      //convert to db  sub
}curveData_t;
typedef struct curveCfg{
    curveParams_t params;
    curveData_t data;
}curveCfg_t;
void curveProcess(int32_t *data,curveCfg_t *cfg,uint16_t wordsCnt);
void curveInit(curveCfg_t *cfg);
void curveReset(curveCfg_t *cfg);
void curveSetParams(curveCfg_t *cfg,curveParams_t *params,int32_t threshold);
void curveGetParams(curveCfg_t *cfg,curveParams_t *params,int32_t *threshold);
/******************** Gain curve Algorithm  End*************************************************** **********/

/******************** Gain Compute Algorithm  End*************************************************** **********/
#define  SMOOTH_PARAM_BIT (ALGOPARAMFACTORBIT)      //Smooth Parameter Expander
#define  SMOOTH_EXPAND (0x7fffffff)                 //Smooth float Convert to Int Coefficient
#define  SMOOTH_OUTDATA_BIT (31)
#define  SMOOTH_INDATA_BIT (20)                     //Smooth Input Data Bit Number
#define  SMOOTH_ALPHA_SCALE (1.12)
typedef struct smoothParams{
    int32_t alphaAttr;
    int32_t alphaRel;
}smoothParams_t;
typedef struct smoothData{
    int32_t state;
    int32_t beta;
    int32_t floatToint;        //input data convert to float coefficient
    uint16_t paramFactorBit;   // parammeter data expandor bit number
    float intToFloat;          //float convert to 32bit interger  coefficient
    float betaCoefficient;     // beta constant coefficient
}smoothData_t;

typedef struct smoothCfg{
    smoothParams_t params;
    smoothData_t data;
}smoothCfg_t;

void smoothProcess(int32_t *data,smoothCfg_t *cfg,uint16_t wordsCnt);
void smoothInit(smoothCfg_t *cfg);
void smoothReset(smoothCfg_t *cfg);
void smoothSetParams(smoothCfg_t *cfg,smoothParams_t *params);
void smoothGetParams(smoothCfg_t *cfg,smoothParams_t *params);
/******************** Gain Compute Algorithm  End*************************************************** **********/

#endif
